<?php
// 应用公共文件
if (!function_exists('current_time')) {
    /**
     * 获取系统当前事件或转换时间戳
     * @param string $time
     * @return false|string
     * @author zyj<535985755@qq.com>
     * Date: 2020/11/1
     */
    function current_time($time = '')
    {
        return $time ? date("Y-m-d H:i:s", $time) : date("Y-m-d H:i:s", time());
    }
}
if (!function_exists('random_string')) {
    /**
     * 生成指定长度的时间戳
     * @param int $len
     * @return string
     * @author zyj<535985755@qq.com>
     * Date: 2020/11/1
     */
    function random_string(int $len)
    {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        mt_srand(10000000 * (double)microtime());
        for ($i = 0, $str = '', $lc = strlen($chars) - 1; $i < $len; $i++) {
            $str .= $chars[mt_rand(0, $lc)];
        }
        return $str;
    }
}

/**
 * base64 安全加密
 * @param $string
 * @return mixed|string
 */
if (!function_exists('urlsafe_b64encode')) {
    function urlsafe_b64encode($string) {
        $result = base64_encode($string);
        $result = str_replace(array('+','/','='),array('-','_',''),$result);
        return $result;
    }
}

if (!function_exists('region')) {
    /**
     * @param string $deep   province city area
     * @return array|mixed
     * @throws throwable
     * @author zyj<535985755@qq.com>
     * Date: 2019/12/20
     */
    function region(string $deep = "")
    {
        $region = \think\facade\Cache::remember('region_' . $deep, function (\app\manage\logic\Region $region) use ($deep) {
            return $region->getRegionData($deep);
        });
        return $region;
    }
}

if (!function_exists('white_signature')) {
    function white_signature($data)
    {
        $arr=array_map(function($v){return is_array($v) ?'':$v;},$data);
        //按照Key排序
        ksort($arr);
        //转字符串
        $sign = array2string($arr, "=", "&");
        //头部追加secret
        $secret = 'kZC2gA9U6p1KsNDMbT4dC';
        $sign = $secret . "&" . $sign;
        return md5($sign);
    }
}

if (!function_exists('array2string')) {
    function array2string($array, $link1, $link2)
    {
        $string = [];
        if ($array && is_array($array)) {
            foreach ($array as $key => $value) {
                $string[] = $key . $link1 . $value;
            }
        }
        return implode($link2, $string);
    }
}